package com.atguigu.es;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
import org.apache.http.HttpHost;
import org.elasticsearch.action.DocWriteResponse;
import org.elasticsearch.action.bulk.BulkItemResponse;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.Test;

import java.io.IOException;
import java.util.Locale;

public class EsTest1 {

    @Test
    public void createIndex() throws IOException {
        // 创建es客户端
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("ubuntu-vm.com", 9200, "http"))
        );
        // 创建索引
        CreateIndexRequest request = new CreateIndexRequest("user");
        CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
        String index = response.index();
        // 响应状态
        boolean acknowledged = response.isAcknowledged();
        System.out.println("响应状态:"+acknowledged);
        System.out.println(index);

        // 关闭es客户端
        client.close();
    }

    @Test
    public void searchIndex() throws IOException {
        // 创建es客户端
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("ubuntu-vm.com", 9200, "http"))
        );
        GetIndexRequest request = new GetIndexRequest("user");
        GetIndexResponse response = client.indices().get(request, RequestOptions.DEFAULT);

        System.out.println("getAliases:"+response.getAliases());
        System.out.println(response.getDataStreams());
        System.out.println(response.getIndices());
        System.out.println(response.getMappings());
        System.out.println(response.getDefaultSettings());
        System.out.println("getSettings:"+response.getSettings());

        // 关闭es客户端
        client.close();
    }

    @Test
    public void insertData() throws IOException {
        // 创建es客户端
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("ubuntu-vm.com", 9200, "http"))
        );

        IndexRequest request = new IndexRequest();
        IndexRequest userIndex = request.index("user").id("1001");

        // 准备数据
        User user = new User();
        user.setAddress("北京市");
        user.setAge(10);
        user.setGender("female");
        user.setName("朱元璋");

        ObjectMapper mapper = new ObjectMapper();
        String valueAsString = mapper.writeValueAsString(user);
        // 向es中插入数据，必须将数据转为json
        request.source(valueAsString, XContentType.JSON);

        IndexResponse response = client.index(request, RequestOptions.DEFAULT);
        DocWriteResponse.Result result = response.getResult();
        String id = response.getId();
        System.out.println(result);
        System.out.println(id);

        // 关闭es客户端
        client.close();
    }

    @Test
    public void updateData() throws IOException {
        // 创建es客户端
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("ubuntu-vm.com", 9200, "http"))
        );

        IndexRequest request = new IndexRequest();
        IndexRequest userIndex = request.index("user").id("1001");

        // 准备数据
        User user = new User();
        user.setAddress("金陵城");
        user.setAge(10);
        user.setGender("female");
        user.setName("李世民");

        ObjectMapper mapper = new ObjectMapper();
        String valueAsString = mapper.writeValueAsString(user);
        // 向es中插入数据，必须将数据转为json
        request.source(valueAsString, XContentType.JSON);

        IndexResponse response = client.index(request, RequestOptions.DEFAULT);
        DocWriteResponse.Result result = response.getResult();
        String id = response.getId();
        System.out.println(result);
        System.out.println(id);

        // 关闭es客户端
        client.close();
    }

    /**
     * 批量插入数据
     * @throws IOException
     */
    @Test
    public void insertBatchData() throws IOException {
        // 创建es客户端
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("ubuntu-vm.com", 9200, "http"))
        );

        // 批量插入数据
        BulkRequest request = new BulkRequest();
        IndexRequest indexRequest1 = new IndexRequest().index("user").id("1002").source(XContentType.JSON,
                "name", "嬴政", "age", 11, "gender", "male", "address", "咸阳城");
        IndexRequest indexRequest2 = new IndexRequest().index("user").id("1003").source(XContentType.JSON,
                "name", "刘邦", "age", 22, "gender", "male", "address", "长安");
        IndexRequest indexRequest3 = new IndexRequest().index("user").id("1004").source(XContentType.JSON,
                "name", "韩信", "age", 33, "gender", "male", "address", "韩国");
        IndexRequest indexRequest4 = new IndexRequest().index("user").id("1005").source(XContentType.JSON,
                "name", "萧何", "age", 55, "gender", "female", "address", "沛县");
        IndexRequest indexRequest5 = new IndexRequest().index("user").id("1006").source(XContentType.JSON,
                "name", "彭越", "age", 44, "gender", "male", "address", "长安城");
        IndexRequest indexRequest6 = new IndexRequest().index("user").id("1007").source(XContentType.JSON,
                "name", "八戒", "age", 77, "gender", "female", "address", "高老庄");
        IndexRequest indexRequest7 = new IndexRequest().index("user").id("1008").source(XContentType.JSON,
                "name", "悟空", "age", 99, "gender", "male", "address", "花果山");
        IndexRequest indexRequest8 = new IndexRequest().index("user").id("1009").source(XContentType.JSON,
                "name", "悟净", "age", 88, "gender", "male", "address", "流沙河");
        request.add(indexRequest1);
        request.add(indexRequest2);
        request.add(indexRequest3);
        request.add(indexRequest4);
        request.add(indexRequest5);
        request.add(indexRequest6);
        request.add(indexRequest7);
        request.add(indexRequest8);

        BulkResponse response = client.bulk(request, RequestOptions.DEFAULT);
        TimeValue took = response.getTook();// 所需时间
        System.out.println(took);
        BulkItemResponse[] items = response.getItems();
        System.out.println(items);

        // 关闭es客户端
        client.close();
    }

    /**
     * 批量删除
     * @throws IOException
     */
    @Test
    public void deleteBatchData() throws IOException {
        // 创建es客户端
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("ubuntu-vm.com", 9200, "http"))
        );

        // 批量删除数据
        BulkRequest request = new BulkRequest();
        DeleteRequest deleteRequest1 = new DeleteRequest().index("user").id("1002");
        DeleteRequest deleteRequest3 = new DeleteRequest().index("user").id("1004");
        request.add(deleteRequest1);
        request.add(deleteRequest3);

        BulkResponse response = client.bulk(request, RequestOptions.DEFAULT);
        TimeValue took = response.getTook();// 所需时间
        System.out.println(took);
        BulkItemResponse[] items = response.getItems();
        System.out.println(items);

        // 关闭es客户端
        client.close();
    }

    @Data
    class User {
        private String name;
        private int age;
        private String gender;
        private String address;
    }
}
